﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Microsoft.Phone.Net.NetworkInformation;

namespace RadarGlobal.UIView
{
    public partial class RegisterDlg : PhoneApplicationPage
    {
        public RegisterDlg()
        {
            InitializeComponent();

            if (NetworkInterface.GetIsNetworkAvailable() != true)

                MessageBox.Show("Can't connect database , Please check wifi/cellular data...");
        }
        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/UIView/LoginDlg.xaml", UriKind.RelativeOrAbsolute));
        }

        private void btnRegister_Click(object sender, RoutedEventArgs e)
        {
            mySQLService.RadarServicePortTypeClient client = new mySQLService.RadarServicePortTypeClient();

            if (NetworkInterface.GetIsNetworkAvailable() != true)
            {
                MessageBox.Show("Can't connect database , Please check wifi/cellular data...");

                return;
            }
            if (txtUsername.Text == "") { MessageBox.Show("Please input username!", "Error", MessageBoxButton.OK); return; }
            if (txtPassword.Password != txtRePassword.Password) { MessageBox.Show("Password are not the same!", "Error", MessageBoxButton.OK); return; }
            if (txtFullname.Text == "") { MessageBox.Show("Please input fullname", "Error", MessageBoxButton.OK); return; }

            client.CreateUserCompleted += (s, args) =>
            {
                if (args.Result == true)
                {
                    MessageBox.Show("Register user sucess!!", "welcom application", MessageBoxButton.OK);
                    NavigationService.Navigate(new Uri("/UIView/LoginDlg.xaml", UriKind.RelativeOrAbsolute));
                }
                else
                    MessageBox.Show("Your account already exists! Please try again", "Error", MessageBoxButton.OK);
            };
            client.CreateUserAsync(txtUsername.Text, txtPassword.Password.ToString(), txtFullname.Text, txtEmail.Text, 1, 1);
        }
    }
}